import { Options } from "./interface/spider";

const http = require('http');
export default class Spider {
  protected options: Options;
  constructor(options: Options) {
    this.options = options;
  };
  start() {
    console.log('开始获取请求网站：' + this.options.url + ',请稍后.....');
    let req = http.request(this.options.url, {
      header: this.options.header || {},
      method: this.options.method || 'get'
    }, (res: any) => {
      let chunks: Uint8Array[] = [];
      res.on('data', (c: Uint8Array) => {
        c && chunks.push(c);
      });
      res.on('end', () => {
        let html = Buffer.concat(chunks).toString('utf-8');
        this.options.callback && this.options.callback(html);
      });
    });
    req.end();
  };
}